package com.sztx.pay.center.core.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sztx.pay.center.dataaccess.dao.mysql.UserInfoMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.redis.UserRedisDAO;
import com.sztx.se.core.service.StartupCallback;
import com.sztx.se.dataaccess.mysql.config.PageQuery;

public class StartupCallbackImpl implements StartupCallback {
	
	private Logger logger = LoggerFactory.getLogger(StartupCallbackImpl.class);
	
	@Autowired
	private UserInfoMysqlDAO userInfoMysqlDAO;	
	@Autowired
	private UserRedisDAO userRedisDAO;

	@Override
	public void businessHandle() {
		
		logger.info("启动完成, 准备初始化数据");
		
		initGrayUser();
	}

	
	
	private void initGrayUser(){		
		logger.info("开始同步灰度用户数据");
		
		int pageNo = 1;
		int pageSize = 5000;	
		int totalGrayUser = 0;
		List<String> grayUserList = null;
		
		PageQuery pageQuery = new PageQuery(pageNo, pageSize);

		
		grayUserList = userInfoMysqlDAO.findGrayUser(pageQuery );
		if (grayUserList != null && !grayUserList.isEmpty()) {
			totalGrayUser += grayUserList.size();
			userRedisDAO.syncGrayUser(grayUserList);			
		}
		
		while (grayUserList != null && grayUserList.size() == pageSize) {
			pageNo++;
			pageQuery = new PageQuery(pageNo, pageSize);
			
			grayUserList = userInfoMysqlDAO.findGrayUser(pageQuery);
			if (grayUserList != null && !grayUserList.isEmpty()) {
				totalGrayUser += grayUserList.size();
				userRedisDAO.syncGrayUser(grayUserList);			
			}
			
		}
		
		logger.info("结束同步灰度用户数据,共"+totalGrayUser+"个灰度用户");		
	}
	
	
	
	
}
